home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / LIB / NDIRWIN.C < prev    next >
C/C++ Source or Header  |  1993-07-31  |  6KB  |  177 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       n d i r w i n . c                                            */
  3. /*                                                                    */
  4. /*       UUPC/extended directory search routine for MS-Windows 3.x    */
  5. /*       environment.                                                 */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*       Changes Copyright (c) 1993 by Robert Denny                   */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  14. /*       Wonderworks.                                                 */
  15. /*                                                                    */
  16. /*       All rights reserved except those explicitly granted by       */
  17. /*       the UUPC/extended license agreement.                         */
  18. /*--------------------------------------------------------------------*/
  19.  
  20. /*--------------------------------------------------------------------*/
  21. /*                          RCS Information                           */
  22. /*--------------------------------------------------------------------*/
  23.  
  24. /*
  25.  *    $Id: ndirwin.c 1.2 1993/07/31 16:22:16 ahd Exp $
  26.  *
  27.  *    Revision history:
  28.  *    $Log: ndirwin.c $
  29.  * Revision 1.2  1993/07/31  16:22:16  ahd
  30.  * Changes in support of Robert Denny's Windows 3.x support
  31.  *
  32.  * Revision 1.1  1993/07/22  23:19:50  ahd
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <dir.h>
  42. #include <dos.h>
  43. #include <errno.h>
  44. #include <time.h>
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    UUPC/extended include files                     */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include "lib.h"
  51. #include "uundir.h"
  52. #include "dos2unix.h"
  53.  
  54. #define USHORT unsigned short
  55.  
  56. static char *pathname = NULL;
  57. static struct ffblk findbuf;
  58.  
  59. currentfile();
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*    o p e n d i r x                                                 */
  63. /*                                                                    */
  64. /*    Open a directory                                                */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. extern DIR *opendirx( const char *dirname, char *pattern)
  68. {
  69.  
  70.    DIR *dirp;
  71.    USHORT rc;
  72.  
  73.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  74.    strcpy(pathname, dirname);
  75.  
  76.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  77.       strcat(pathname,"/");
  78.    strcat(pathname,pattern);
  79.    printmsg(5,"opendir: Opening directory %s", pathname );
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*                Read the first file in the directory                */
  83. /*--------------------------------------------------------------------*/
  84.  
  85.    rc = findfirst( pathname, &findbuf, FA_NORMAL);
  86.  
  87. /*--------------------------------------------------------------------*/
  88. /*            Process the return code from the first file             */
  89. /*--------------------------------------------------------------------*/
  90.  
  91.    if ( rc == 0 )
  92.    {
  93.       dirp = malloc( sizeof( DIR ));
  94.       dirp->dirfirst = 1;
  95.       strcpy(dirp->dirid, "DIR");
  96.       return dirp;
  97.    }
  98.    else
  99.    {
  100.           if (( rc != ENMFILE ) && ( rc != ENOENT ))
  101.          printmsg(4,"opendir: Error %d on directory %s",
  102.                   (int) rc, pathname );
  103.       return NULL;
  104.    } /* else */
  105.  
  106. } /*opendir*/
  107.  
  108. /*--------------------------------------------------------------------*/
  109. /*    r e a d d i r                                                   */
  110. /*                                                                    */
  111. /*    Get next entry in a directory                                   */
  112. /*--------------------------------------------------------------------*/
  113.  
  114. struct direct *readdir(DIR *dirp)
  115. {
  116.    USHORT rc = 0;
  117.  
  118.    if ( ! equal(dirp->dirid, "DIR" ))
  119.    {
  120.       printmsg(0,"readdir: No directory open to read");
  121.       panic();
  122.    }
  123.  
  124.    if (dirp->dirfirst)
  125.    {
  126.       printmsg(5,"readdir: Opening directory %s", pathname );
  127.       dirp->dirfirst = 0;
  128.    }
  129.    else
  130.           rc = findnext(&findbuf);
  131.  
  132.    if ( rc == 0 )
  133.    {
  134.       dirp->dirent.d_ino = -1;   /* no inode information */
  135.       strlwr(strcpy(dirp->dirent.d_name, findbuf.ff_name ));
  136.           dirp->dirent.d_namlen = strlen(findbuf.ff_name);
  137.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  138.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  139.  
  140.       dirp->dirent.d_modified = dos2unix( *((FDATE *) &findbuf.ff_fdate),
  141.                                           *((FTIME *) &findbuf.ff_ftime));
  142.  
  143.       printmsg(4,"readdir: Returning \"%s\"", dirp->dirent.d_name);
  144.       return &(dirp->dirent);
  145.    }
  146.    else {
  147.           if (( errno != ENMFILE ) && ( errno != ENOENT ))
  148.          printmsg(0,"readdir: Error %d on directory %s",
  149.                   (int) rc, pathname );
  150.       return NULL;
  151.    } /* else */
  152.  
  153. } /*readdir*/
  154.  
  155. /*--------------------------------------------------------------------*/
  156. /*    c l o s e d i r                                                 */
  157. /*                                                                    */
  158. /*    Close a directory                                               */
  159. /*--------------------------------------------------------------------*/
  160.  
  161. void closedir(DIR *dirp)
  162. {
  163.  
  164.    if ( ! equal(dirp->dirid, "DIR" ))
  165.    {
  166.       printmsg(0,"closedir: No directory open");
  167.       panic();
  168.    }
  169.  
  170.    printmsg(5,"closedir: Closing directory %s", pathname );
  171.    free( dirp );
  172.    dirp = NULL;
  173.    free( pathname );
  174.    pathname = NULL;
  175.  
  176. } /*closedir*/
  177.